package com.atguigu.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.feign.FeignMemberService;
import com.atguigu.gulimall.ware.vo.FareRespVo;
import com.atguigu.gulimall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareInfoDao;
import com.atguigu.gulimall.ware.entity.WareInfoEntity;
import com.atguigu.gulimall.ware.service.WareInfoService;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                new QueryWrapper<WareInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Autowired
    FeignMemberService feignMemberService;

    @Override
    public FareRespVo getFare(Long addrId) {
        R r = feignMemberService.addrInfo(addrId);
        FareRespVo fareRespVo = new FareRespVo();
        //memberReceiveAddress
        MemberAddressVo memberReceiveAddress = r.getData("memberReceiveAddress", new TypeReference<MemberAddressVo>() {
        });
        if (memberReceiveAddress != null) {
            String phone = memberReceiveAddress.getPhone();
            String substring = phone.substring(phone.length() - 1, phone.length());
            BigDecimal farePrice = new BigDecimal(substring);
            fareRespVo.setAddress(memberReceiveAddress);
            fareRespVo.setFarePrice(farePrice);
            return fareRespVo;

        }

        return null;
    }

}